package com.example.phpclient.content;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.VideoView;

import com.example.phpclient.DB.NotesDB;
import com.example.phpclient.R;

import java.io.File;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AddContent extends AppCompatActivity implements View.OnClickListener {

    private String val;
    private Button savebtn, deletebtn;

    private EditText ettext;
    private ImageView c_img;
    private VideoView c_video;
    private NotesDB notesDB;
    private SQLiteDatabase dbWiter;
    private File phoneFile,videoFile;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addcontent_activity);

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        //接收传值

        val = getIntent().getStringExtra("flag");//flag的值判断没写错

        savebtn = (Button) findViewById(R.id.save);
        deletebtn = (Button) findViewById(R.id.delete);

        //添加界面所显示的图片文字
        ettext = (EditText) findViewById(R.id.ettext);
        c_img = (ImageView) findViewById(R.id.c_img);
        c_video = (VideoView) findViewById(R.id.c_video);

        savebtn.setOnClickListener(this);
        deletebtn.setOnClickListener(this);

        notesDB = new NotesDB(this);
        dbWiter = notesDB.getWritableDatabase();
        initView();
    }

    public void initView(){
        if (val.equals("1")){//文字
            c_img.setVisibility(View.GONE);//隐藏img
            c_video.setVisibility(View.GONE);
        }
        if (val.equals("2")){//图片
            c_img.setVisibility(View.VISIBLE);//显示img
            c_video.setVisibility(View.GONE);
            Intent iimg =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用系统相机拍照
            //把图片放到SD卡，数据库仅存储path
            //绝对路径，时间命名
            phoneFile =new File(Environment.getExternalStorageDirectory().getAbsoluteFile()
                    +"/"+getTime()+".jpg");
            iimg.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(phoneFile));
            startActivityForResult(iimg,1);

        }
        if (val.equals("3")){//视频
            c_video.setVisibility(View.VISIBLE);//显示video
            c_img.setVisibility(View.GONE);
            Intent ivideo =new Intent(MediaStore.ACTION_VIDEO_CAPTURE);//调用系统相机拍照
            //把图片放到SD卡，数据库仅存储path
            //绝对路径，时间命名
            videoFile =new File(Environment.getExternalStorageDirectory().getAbsoluteFile()
                    +"/"+getTime()+".mp4");
            ivideo.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoFile));
            startActivityForResult(ivideo,2);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.save:
                addDB(); //保存时调用添加数据的操作
                finish();
                break;
            case R.id.delete:
                finish();
                break;
        }

    }
    //向数据库添加内容

    public void addDB() {
        ContentValues cv = new ContentValues();
        cv.put(NotesDB.CONTENT, ettext.getText().toString());
        cv.put(NotesDB.TIME, getTime());
        cv.put(NotesDB.PATH, phoneFile+"");//图片插入数据库的是路径
        cv.put(NotesDB.VIDEO, videoFile+"");
        dbWiter.insert(NotesDB.TABLE_NAME, null, cv);
    }

    private String getTime() {
        SimpleDateFormat format = new SimpleDateFormat("MMMM_dd_yyyy_h_mmaa");
        Date date = new Date();
        String str = format.format(date);
        return str;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1)
        {//如果等于1代表传递的是一个图片
            Bitmap bitmap = BitmapFactory.decodeFile(phoneFile.getAbsolutePath());
            c_img.setImageBitmap(bitmap);
        }
        if (requestCode == 2)
        {
            c_video.setVideoURI(Uri.fromFile(videoFile));
            c_video.start();
        }
    }
}
